import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/dialog/exit_tips.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/application/special_list/process_nodes_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/special_operation/special_list/process_allocation_page/process_allocation_apply_page/process_allocation_apply_controller.dart';

/// 人员配置选择模板后
class ProcessAllocationApplyPage extends StatelessWidget {
  ProcessAllocationApplyPage({super.key});

  final controller = Get.put(ProcessAllocationApplyController());

  //选择样式
  Widget _getSelectView(ProcessNodesEntity entity) {
    return Row(
      children: [
        Text(
          entity.userTypeName ?? '',
          style: TextStyle(
            fontSize: 14.sp,
            height: 1.1,
            color: AppColors.font_color_black,
          ),
        ),
        AppGaps.hGap20,
        Expanded(
            child: Container(
          height: 50.w,
          alignment: Alignment.centerRight,
          child: entity.userIds!.isNotEmpty && entity.userIds![0].getShowName() != ''
              ? Text(
                  entity.userIds!.map((e) => e.getShowName).join(','),
                  style: TextStyle(
                    fontSize: 14.sp,
                    height: 1.2,
                    color: const Color(0xFF262F38),
                  ),
                )
              : Text(
                  entity.startingCanChanged ?? true ? '请选择' : '暂无',
                  style: TextStyle(
                    fontSize: 14.sp,
                    height: 1.2,
                    color: AppColors.text_grey,
                  ),
                ),
        )),
        Visibility(
            visible: entity.startingCanChanged ?? true,
            child: Row(
              children: [
                AppGaps.hGap6,
                Icon(Icons.arrow_forward_ios, size: 14.sp, color: AppColors.text_grey)
              ],
            ))
      ],
    ).clickOnce(() {
      //不可点击
      if (!entity.startingCanChanged!) {
        return;
      }
      controller.selectUser(entity);
    });
  }

  // 配置弹窗
  void _buildConfigDialog(ProcessNodesEntity entity) {
    Get.bottomSheet(Container(
      alignment: Alignment.center,
      child: Container(
        constraints: BoxConstraints(
          maxHeight: 600.w,
          minHeight: 400.w,
        ),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.only(topLeft: Radius.circular(20.w), topRight: Radius.circular(20.w)),
          gradient: const LinearGradient(
              //渐变位置
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              //渐变颜色[始点颜色, 结束颜色]
              colors: [
                Color(0xffE7EAFF),
                Color(0xffffffff),
                Color(0xffffffff),
                Color(0xffffffff),
              ]),
        ),
        child: Stack(
          alignment: Alignment.center,
          children: [
            Container(
                padding: EdgeInsets.symmetric(horizontal: 16.w),
                child: Column(
                  children: [
                    AppGaps.vGap20,
                    Row(
                      children: [
                        Text(
                          '配置',
                          style: TextStyle(color: Colors.black, fontSize: 18.sp, fontWeight: FontWeight.bold),
                        ),
                        Icon(
                          Icons.help,
                          size: 18.w,
                        ).click(_buildExplainDialog)
                      ],
                    ),
                    AppGaps.vGap20,
                    _getSelectView(entity),
                    const Spacer(),
                    SizedBox(
                      width: 270.w,
                      height: 40.w,
                      child: ElevatedButton(
                        style: ButtonStyle(
                          backgroundColor: MaterialStateProperty.all(const Color(0xff5C6DFF)),
                        ),
                        child: Text(
                          '确定',
                          style: TextStyle(fontSize: 16.sp, color: Colors.white),
                        ),
                        onPressed: () {
                          Navigator.pop(Get.context!);
                        },
                      ),
                    ),
                    AppGaps.vGap10,
                  ],
                )),
            Positioned(
                top: 8,
                right: 8,
                child: IconButton(
                  icon: Container(
                    width: 30.w,
                    height: 30.w,
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.all(Radius.circular(15.w)),
                    ),
                    child: Icon(
                      Icons.close,
                      color: Colors.grey,
                      size: 21.w,
                    ),
                  ),
                  onPressed: () {
                    Navigator.pop(Get.context!);
                  },
                )),
          ],
        ),
      ),
    ));
  }

  // 说明弹窗
  _buildExplainDialog() {
    Get.dialog(ExitTips(
      title: '说明',
      content: '若需要调整或自定义表单请前往xx系统-xx模块',
      func: (b) {},
    ));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar(
        titleText: controller.processListEntity.templateName,
      ),
      body: GetBuilder(
        init: controller,
        builder: (_) {
          if (controller.nodeList.isEmpty) {
            return Container();
          }
          return Column(
            children: [
              Container(
                height: 14.w,
                color: AppColors.bg_color_grey,
              ),
              Container(
                height: 2.w,
                color: AppColors.blue_button,
              ),
              AppGaps.vGap18,
              Expanded(
                  child: Stack(
                children: [
                  Container(
                    width: 1.w,
                    margin: EdgeInsets.only(left: 34.5.w),
                    height: (controller.nodeList.length - 3) * 78.w,
                    decoration: BoxDecoration(
                        borderRadius: BorderRadius.all(Radius.circular(2.w)),
                        gradient: const LinearGradient(
                            //渐变位置
                            begin: Alignment.topCenter,
                            end: Alignment.bottomCenter,
                            //渐变颜色[始点颜色, 结束颜色]
                            colors: [
                              Colors.white,
                              Color(0xff557CFD),
                              Color(0xff557CFD),
                              Color(0xff3BABD8),
                              Colors.white,
                            ])),
                    // height: 10.w,
                  ),
                  ListView.builder(
                    itemCount: controller.nodeList.length,
                    // physics: const NeverScrollableScrollPhysics(),
                    itemBuilder: (context, index) {
                      if (controller.nodeList[index].nodeType == 3 ||
                          controller.nodeList[index].nodeType == 4) {
                        return Container();
                      }
                      return SizedBox(
                        height: 77.w,
                        child: Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            AppGaps.hGap30,
                            Container(
                              width: 10.w,
                              height: 10.w,
                              margin: EdgeInsets.only(top: 5.w),
                              decoration: BoxDecoration(
                                color: AppColors.blue_button,
                                borderRadius: BorderRadius.circular(5.w),
                              ),
                              child: Container(
                                width: 6.w,
                                height: 6.w,
                                margin: EdgeInsets.all(2.w),
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(3.w),
                                ),
                              ),
                            ),
                            AppGaps.hGap10,
                            Container(
                                width: 16.w,
                                height: 16.w,
                                margin: EdgeInsets.only(top: 3.w),
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                  color: AppColors.primary_color,
                                  borderRadius: BorderRadius.circular(8.w),
                                ),
                                child: Text(
                                  '$index',
                                  textAlign: TextAlign.center,
                                  style: TextStyle(color: Colors.white, fontSize: 10.sp),
                                )),
                            AppGaps.hGap6,
                            Text(
                              controller.nodeList[index].nodeName ?? '',
                              style: TextStyle(
                                fontSize: 16.sp,
                                fontWeight: FontWeight.w500,
                              ),
                            ),
                          ],
                        ),
                      ).click(() {
                        // 业务变动,暂时需要再选择节点执行人,
                        // _buildConfigDialog(controller.nodeList[index]);
                      });
                    },
                  ),
                ],
              )),
              AppGaps.vGap60,
            ],
          );
        },
      ),
      bottomSheet: Container(
        height: 50.w,
        width: double.infinity,
        color: Colors.white,
        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 5.w),
        child: ElevatedButton(
          style: ElevatedButton.styleFrom(
            backgroundColor: AppColors.blue_button,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(22.r),
            ),
          ),
          onPressed: controller.toApproval,
          child: Text(
            '下一步',
            style: TextStyle(
              fontSize: 16.sp,
              color: Colors.white,
            ),
          ),
        ),
      ),
    );
  }
}
